package com.lge.clock.test;

import com.lge.clock.WorldClockActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Instrumentation;
import android.app.Instrumentation.ActivityMonitor;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.test.ActivityInstrumentationTestCase2;
import android.test.TouchUtils;
import android.text.format.DateFormat;
import android.view.KeyEvent;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TextView;

import com.lge.clock.R;
import com.lge.clock.alarmclock.ALSettings;
import com.lge.inner_sui.widget.control.SUIDrumTimePicker;
import com.lge.clock.worldclock.cities.CityDBManager;
import com.lge.clock.worldclock.util.WCPreferences;

public class WorldClockActivityTest_checkingTimeUpdate extends ActivityInstrumentationTestCase2<WorldClockActivity> {

	private Instrumentation inst;
	private ActivityMonitor monitor;
	private Activity currentActivity;
	private TheBroadcastReceiver intentReceiver;
	private CityDBManager mCityManager;
	private boolean isMinuteChanged;
	
	public WorldClockActivityTest_checkingTimeUpdate() {
		// TODO Auto-generated constructor stub
		super("com.lge.clock", WorldClockActivity.class);
	}

	@Override
	protected void setUp() throws Exception {
		// TODO Auto-generated method stub
		super.setUp();
		
		inst = getInstrumentation();
		IntentFilter filter = null;
		monitor = inst.addMonitor(filter, null, false);
		
		isMinuteChanged = false;
		
		currentActivity = getActivity();
		
		intentReceiver = new TheBroadcastReceiver();
		IntentFilter filterBR = new IntentFilter(); 
		filterBR.addAction(Intent.ACTION_TIME_TICK);	
		currentActivity.registerReceiver(intentReceiver, filterBR); 	
		
	}
	
	@Override
	protected void tearDown() throws Exception {
		// TODO Auto-generated method stub
		mCityManager.deleteAllCity();
		currentActivity.finish();
		super.tearDown();
	}
	
		
	/**
	 * testWorldClockTimeUpdate() @throws Exception
 	 * 1. add 1 city to check the time-flying
 	 * 2. get the minute value in TimePicker as old one. 
 	 * 3. when ACTION_TIME_TICK is invoked, get the minute value in TimePicker as new one.
 	 * 4. compare the old one to the new one.
 	 * 5. true if the difference is 1 (minute) 
	 */
	public void testWorldClockTimeUpdate() throws Exception {
		//clickButtonById(R.id.noListAddButton);
		/* enabling ListSearchActivity */
		WCPreferences wcPref = new WCPreferences(currentActivity);
		wcPref.setSearchType(false); // enabling ListSearchActivity when click Add-Button

		clickButtonById(com.lge.clock.R.id.Btn_City);
		
		mCityManager = CityDBManager.getInstance(monitor.getLastActivity());		
        mCityManager.addCityInfo(1);
        
        inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
        inst.waitForIdleSync();
//        oldMinute = mCityManager.getCityInfo(1).
        
        TextView minuteTextView = (TextView)currentActivity.findViewById(com.lge.clock.R.id.DClockMinute);
        int oldMinute = Integer.valueOf((String) minuteTextView.getText());
        
        int count = 0;
        while(true) {
        	count++;
        	if (isMinuteChanged || count > 60) break;
        		
        	try {
				Thread.sleep(1000);
			} catch (Exception e) {
				// TODO: handle exception
			}
        }
		
        int newMinute = Integer.valueOf((String) minuteTextView.getText());
        System.out.println("oldMinute : "+oldMinute+"newMinute : "+newMinute);
        assertEquals(oldMinute+1, newMinute);
        
        count = 0;
        while(true) {
        	count++;
        	if (count > 2) break;
        		
        	try {
				Thread.sleep(1000);
			} catch (Exception e) {
				// TODO: handle exception
			}
        }
	}
	
	public void clickButtonById(int id) {
		Button button = (Button)currentActivity.findViewById(id);
		TouchUtils.clickView(this, button);
		inst.waitForIdleSync();
	}
	
	public class TheBroadcastReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			if (intent.getAction().equals(Intent.ACTION_TIME_TICK)) {
				isMinuteChanged = true;
			}
		}
	}
}
